import { defineStore } from "pinia";
import { computed, ref } from "vue";

import { useInfoStore } from "./info.js";
/*
    defineStore 定义状态
    1. id参数必须要写，显示在vuetool中
    2. 配置选项，或者是一个setup
    返回值
    1. 返回一个函数
    2. 执行函数函数返回状态对象
    访问状态
    1. 属性访问：状态对象.属性
    2. 方法调用：状态对象.方法()
    命名
    1· useAaBbStroe
*/

export const useShopStore = defineStore("shop", {
  state() {
    return {
      title: "hello shop",
      age: 10,
    };
  },
  getters: {
    count: (state) => state.title + 1000,
    time: (state) => {
      return (str) => {
        return `${str}:${state.age}-${state.title}-${Math.random()}`;
      };
    },
    num: () => {
      const infoStore = useInfoStore();
      return infoStore.count + 500;
    },
  },
  actions: {
    say() {
      console.log(this.title + "hello");
    },
  },
  persist: {
    enabled: true,
  },
});

export const useCartStore = defineStore(
  "cart",
  () => {
    const infoStore = useInfoStore();
    const title = ref("hello shop"); // state
    const count = ref(0); // state
    const dblCount = computed(() => {
      return count * 2; // getter
    });
    function say() {
      //action
      // count.value++;
      // console.log("hello cart");
      console.log(infoStore.say());
    }
    return { title, count, dblCount, say };
  },
  {
    persist: {
      enabled: true,
    },
  }
);
